# -*- coding: utf-8 -*-

try:
    from spider import Gather, Parser
except ImportError:
    from __init__ import Gather, Parser

import gzip
try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

class SEO(Gather, Parser):

    def __init__(self, domain):
        self.title = None
        self.description = None
        self.keywords = None

        Gather.__init__(self, domain)
        Parser.__init__(self, self.gather())

    def parser(self):
        soup = self._soup()

        title = soup.title
        if title:
            self.title = title.text

        soup_keywords = soup.find("meta", attrs={"name": "keywords"})
        if not soup_keywords:
            soup_keywords = soup.find("meta", attrs={"name": "Keywords"})
        if soup_keywords:
            self.keywords = soup_keywords.attrs["content"]

        soup_description = soup.find("meta", attrs={"name": "description"})
        if not soup_description:
            soup_description = soup.find("meta", attrs={"name": "Description"})
        if soup_description:
            self.description = soup_description.attrs["content"]

if __name__ == '__main__':
    seo = SEO("http://www.baidu.com")
    seo.parser()
    print seo.title
    print seo.keywords
    print seo.description
    #html = StringIO.StringIO(seo.description.encode("utf-8"))
    #gzhtml = gzip.GzipFile(fileobj=html)
    #ungzhtml = gzhtml.read()
    #gzhtml.close()
    #print ungzhtml
